package cc.linker.steplink.crm.merchant.controller;

import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.merchant.service.AuthService;
import cc.linker.steplink.crm.response.GlobalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/auth")
public class AuthController {

    @Autowired
    private AuthService authService;
    @Autowired
    private UserInfoUtil userInfoUtil;


    /**
     * @Description: 获取授权信息
     * @Param: [orgId 机构id]
     * @Return: java.util.Map<java.lang.String, java.lang.Object>
     * @Author: ch
     * @Date: 2019/11/27 8:53
     * @Version:1.11.10
     **/
    @GetMapping("/getAuthInfo")
    public Map<String, Object> getAuthInfo(String orgId) {
        return authService.getAuthInfo(orgId);
    }


    /**
     * @Description: 保存授权信息
     * @Param: orgId 机构id
     * @Param: cusNum 客户专员机器人数量
     * @Param: cusMaxNum 最大客户专员机器人数量
     * @Param: cusExpireDate 客户专员机器人到期日期
     * @Param: marNum 营销机器人数
     * @Param: marMaxNum 最大营销机器人数
     * @Param: marExpireDate 营销机器人到期日期
     * @Param: saleNum 销售机器人数
     * @Param: saleMaxNum 最大销售机器人数
     * @Param: saleExpireDate 销售机器人到期日期
     * @Param: cpwNum 文案助手数
     * @Param: cpwMaxNum 最大文案助手数
     * @Param: cpwExpireDate 文案助手到期日期
     * @Param: chaNum 渠道推广员数
     * @Param: chaMaxNum 最大渠道推广员数
     * @Param: chaExpireDate 渠道推广员到期日期
     * @Param: joinNum 加盟代理数
     * @Param: joinMaxNum 最大加盟代理数
     * @Param: joinExpireDate 加盟代理到期日期
     * @Param: uniNum 联盟商家数
     * @Param: uniMaxNum 最大联盟商家数
     * @Param: uniMaxGoodsNum 最大联盟商品数
     * @Param: uniExpireDate 联盟商家到期日期
     * @Return: void
     * @Author: ch
     * @Date: 2019/11/27 8:53
     * @Version:
     **/
    @PostMapping("/saveAuth")
    public void saveAuth(@RequestBody Map<String, Object> map, HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        if (!map.containsKey("orgId") || StringUtils.isEmpty(map.get("orgId"))) {
            map.put("orgId", userinfo.getAdminId());
        }
        authService.saveAuth(map);
    }

    @GetMapping("/getSalerList")
    public Object getSalerList(@RequestParam(value = "pageIndex") Integer pageIndex,
                               @RequestParam(value = "pageSize") Integer pageSize,
                               @RequestParam(value = "account") String account,
                               @RequestParam(value = "orgId") String orgId ) {
        return authService.getSalerList(pageIndex, pageSize, account, orgId);
    }

    //获取应用列表
    @GetMapping("/getapplicationlist")
    public List<Map<String,Object>> getApplicationList(){
        return authService.getApplicationList();
    }

    //获取应用详情
    @GetMapping("/getapplicationinfo")
    public Map<String,Object> getApplicationInfo(String id){
        return authService.getApplicationInfo(id);
    }

    //编辑应用详情
    @PostMapping("/saveapplication")
    public void saveApplication(@RequestBody Map<String, Object> map){
        authService.saveApplication(map);
    }

    @GetMapping("/applicationAuth")
    public Map get(String applicationAlias,String userId){
        String orgId = userId;
        //判断是否为小b
        Integer count = authService.getSalerCount(userId);
        if (count==0){
            orgId = authService.getOrgIdByUserId(userId);
        }
        if(StringUtils.isEmpty(orgId)){
            throw GlobalException.build("用户不存在！");
        }
        String column = "EPA_OPEN";
        String timeColumn = "EPA_EXPIRE_DATE";
        if("wzkh".equals(applicationAlias)){
            column = "TSA_OPEN";
            timeColumn = "TSA_EXPIRE_DATE";
        }
        Map map = new HashMap();
        map.put("column",column);
        map.put("orgId",orgId);
        map.put("timeColumn",timeColumn);
        //是否开通
        Integer isOpen = authService.getIsOpen(map);
        if(isOpen==null){
            authService.insertAuth(orgId);
            isOpen = authService.getIsOpen(map);
        }
        Map<String, Object> resMap = authService.getApplicationInfoByAlias(applicationAlias);
        if(resMap==null){
            throw GlobalException.build("暂无该应用！");
        }
        resMap.put("isOpen",isOpen);
        return resMap;
    }

    @GetMapping("/applicationInfo")
    public Map getInfo(String applicationAlias,HttpServletRequest request){
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String userId = userinfo.getAdminId();
        boolean ifsales = userinfo.getSaler();
        String orgId = userId;
        //判断是否为小b
        if (!ifsales){
            //不是小b
            orgId = userinfo.getOrgId();
        }
        if(StringUtils.isEmpty(orgId)){
            throw GlobalException.build("用户不存在！");
        }
        String column = "EPA_OPEN";
        String timeColumn = "EPA_EXPIRE_DATE";
        if("wzkh".equals(applicationAlias)){
            column = "TSA_OPEN";
            timeColumn = "TSA_EXPIRE_DATE";
        }else if("yxjqr".equals(applicationAlias)){
            column = "MAR_OPEN";
            timeColumn = "MAR_EXPIRE_DATE";
        }else if("khzy".equals(applicationAlias)){
            column = "CUS_OPEN";
            timeColumn = "CUS_EXPIRE_DATE";
        }else if("wwzb".equals(applicationAlias)){
            column = "WWL_OPEN";
            timeColumn = "WWL_EXPIRE_DATE";
        }else if("ghs".equals(applicationAlias)){
            column = "SUP_OPEN";
            timeColumn = "SUP_EXPIRE_DATE";
        }
        Map map = new HashMap();
        map.put("column",column);
        map.put("orgId",orgId);
        map.put("timeColumn",timeColumn);
        //是否开通
        Integer isOpen = authService.getIsOpen(map);
        if(isOpen==null){
            authService.insertAuth(orgId);
            isOpen = authService.getIsOpen(map);
        }
        Map<String, Object> resMap = authService.getApplicationInfoByAlias(applicationAlias);
        if(resMap==null){
            throw GlobalException.build("暂无该应用！");
        }
        resMap.put("isOpen",isOpen);
        return resMap;
    }

}
